#include <utils.h>
#include <string.h>

float const sin200[200] = {0.000000,0.031411,0.062791,0.094108,0.125333,0.156434,0.187381,0.218143,0.248690,0.278991\
,0.309017,0.338738,0.368125,0.397148,0.425779,0.453991,0.481754,0.509042,0.535827,0.562083\
,0.587785,0.612907,0.637424,0.661312,0.684547,0.707107,0.728969,0.750111,0.770513,0.790155\
,0.809017,0.827081,0.844328,0.860742,0.876307,0.891007,0.904827,0.917755,0.929777,0.940881\
,0.951057,0.960294,0.968583,0.975917,0.982287,0.987688,0.992115,0.995562,0.998027,0.999507\
,1.000000,0.999507,0.998027,0.995562,0.992115,0.987688,0.982287,0.975917,0.968583,0.960294\
,0.951056,0.940881,0.929776,0.917754,0.904827,0.891006,0.876306,0.860742,0.844328,0.827080\
,0.809017,0.790155,0.770513,0.750111,0.728968,0.707106,0.684547,0.661311,0.637423,0.612906\
,0.587785,0.562083,0.535826,0.509041,0.481753,0.453990,0.425779,0.397147,0.368124,0.338737\
,0.309016,0.278990,0.248689,0.218142,0.187380,0.156433,0.125332,0.094107,0.062790,0.031410\
,-0.000001,-0.031412,-0.062792,-0.094109,-0.125334,-0.156436,-0.187382,-0.218144,-0.248691,-0.278992\
,-0.309018,-0.338739,-0.368126,-0.397149,-0.425780,-0.453992,-0.481755,-0.509042,-0.535828,-0.562084\
,-0.587786,-0.612908,-0.637425,-0.661313,-0.684548,-0.707108,-0.728970,-0.750112,-0.770514,-0.790156\
,-0.809018,-0.827081,-0.844329,-0.860743,-0.876307,-0.891007,-0.904828,-0.917755,-0.929777,-0.940881\
,-0.951057,-0.960294,-0.968583,-0.975917,-0.982288,-0.987689,-0.992115,-0.995562,-0.998027,-0.999507\
,-1.000000,-0.999507,-0.998027,-0.995562,-0.992115,-0.987688,-0.982287,-0.975916,-0.968583,-0.960293\
,-0.951056,-0.940880,-0.929776,-0.917754,-0.904826,-0.891006,-0.876306,-0.860741,-0.844327,-0.827080\
,-0.809016,-0.790154,-0.770512,-0.750110,-0.728967,-0.707106,-0.684546,-0.661311,-0.637423,-0.612906\
,-0.587784,-0.562082,-0.535825,-0.509040,-0.481752,-0.453989,-0.425778,-0.397146,-0.368123,-0.338736\
,-0.309015,-0.278989,-0.248688,-0.218141,-0.187379,-0.156432,-0.125331,-0.094106,-0.062788,-0.031409};

float const cos200[] = {1.000000,0.999507,0.998027,0.995562,0.992115,0.987688,0.982287,0.975917,0.968583,0.960294\
,0.951057,0.940881,0.929776,0.917755,0.904827,0.891006,0.876307,0.860742,0.844328,0.827081\
,0.809017,0.790155,0.770513,0.750111,0.728968,0.707107,0.684547,0.661312,0.637424,0.612907\
,0.587785,0.562083,0.535827,0.509041,0.481753,0.453990,0.425779,0.397148,0.368124,0.338738\
,0.309017,0.278991,0.248689,0.218143,0.187381,0.156434,0.125333,0.094108,0.062790,0.031410\
,-0.000001,-0.031411,-0.062791,-0.094109,-0.125334,-0.156435,-0.187382,-0.218144,-0.248690,-0.278992\
,-0.309018,-0.338739,-0.368125,-0.397149,-0.425780,-0.453991,-0.481754,-0.509042,-0.535827,-0.562084\
,-0.587786,-0.612908,-0.637425,-0.661312,-0.684548,-0.707107,-0.728969,-0.750112,-0.770514,-0.790155\
,-0.809017,-0.827081,-0.844328,-0.860742,-0.876307,-0.891007,-0.904827,-0.917755,-0.929777,-0.940881\
,-0.951057,-0.960294,-0.968583,-0.975917,-0.982287,-0.987688,-0.992115,-0.995562,-0.998027,-0.999507\
,-1.000000,-0.999507,-0.998027,-0.995562,-0.992115,-0.987688,-0.982287,-0.975917,-0.968583,-0.960293\
,-0.951056,-0.940880,-0.929776,-0.917754,-0.904827,-0.891006,-0.876306,-0.860741,-0.844327,-0.827080\
,-0.809016,-0.790154,-0.770512,-0.750110,-0.728968,-0.707106,-0.684546,-0.661311,-0.637423,-0.612906\
,-0.587784,-0.562082,-0.535826,-0.509040,-0.481753,-0.453989,-0.425778,-0.397147,-0.368123,-0.338737\
,-0.309016,-0.278990,-0.248689,-0.218142,-0.187380,-0.156433,-0.125332,-0.094107,-0.062789,-0.031409\
,0.000001,0.031412,0.062792,0.094110,0.125335,0.156436,0.187383,0.218145,0.248691,0.278993\
,0.309018,0.338739,0.368126,0.397149,0.425781,0.453992,0.481755,0.509043,0.535828,0.562085\
,0.587787,0.612908,0.637425,0.661313,0.684548,0.707108,0.728970,0.750112,0.770514,0.790156\
,0.809018,0.827082,0.844329,0.860743,0.876308,0.891007,0.904828,0.917755,0.929777,0.940881\
,0.951057,0.960294,0.968584,0.975917,0.982288,0.987689,0.992115,0.995562,0.998027,0.999507};

void InitPassword(PasswordDef * password, char *value){
    char * p = value;
    int32_t i = 0;
    while(*p){
        password->next[i] = 0;
        password->v[i] = *p;
        ++p;
        ++i;
    }
	password->v[i] = 0;
    password->length = i;
}

char checkPassword(PasswordDef* password, char ch){
    int32_t i;
    if(password->next[password->length-2]>0 && ch == password->v[password->length-1])
    {
        InitPassword(password, password->v);
        return 1; // Password match;
    }
    for(i=password->length-3;i>=0;--i){
        if(password->next[i] > 0){
            password->next[i] = 0;
            if(password->v[i+1] == ch){ password->next[i+1] = 1; }
        }
    }
    if(password->v[0] == ch)password->next[0] = 1;
    return 0;
}

int16_t toInt16(char* data)
{
    int16_bytes conv;
    strcpy(conv.bytes, data);
    return conv.value;
}

int32_t toInt32(char* data)
{
    int32_bytes conv;
    strcpy(conv.bytes, data);
    return conv.value;
}

float tofloat(char* data)
{
    int32_bytes conv;
    strcpy(conv.bytes, data);
    return conv.value;
}
